/**
 * 商家列表模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	
	var pageSize=10;
	
	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
	

	
	var validate = require("../../custom/base/component-validate.js");
	
	var async = require("../../custom/base/component-async.js");

	require("../../custom/base/component-core.js");
	
	require("../../custom/base/component-map.js");

	require("../../custom/base/component-page.js");

	
	//初始化函数
	exports.init=function(m){
	
		window.$ldh_shop_list=m;
				
		
		base.setFromPage("shopList");
		
		exports.setShopType(base.getParameter("shopTypeId"));
				
		exports.getShopTypeByParentId();
		
		exports.getSpeTypes();

		//document.title="云便利 附近商家列表";
		
	};
	
	//设置列表页码
	exports.setCurrentPage=function(currentPage){
		
		this.currentPage=currentPage;
		
	};
	
	//获取列表页码
	exports.getCurrentPage=function(){
		
		return this.currentPage==null?1:this.currentPage;
		
	};
	

	//设置排序帅选
	exports.setSort=function(sort,sortName){
		
		this.sort=sort;
		
		$("#sortName").html(base.subString(sortName,3));
		
		$("#ldhStoreList .storelist_rank").removeClass("on");
		
		$("#ldhStoreList .checkmore").hide();
		
		$("#shopList").html("");
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
	};
	
	//获取排序帅选
	exports.getSort=function(){
		
		return this.sort==null?"2":this.sort;
		
	};
	
	//设置店铺状态帅选
	exports.setBusinessStatus=function(businessStatus,businessStatusName){
		
		this.businessStatus=businessStatus;
		
		$("#businessStatusName").html(businessStatusName);
		
		$("#ldhStoreList .storelist_classify").removeClass("on");
				
		$("#ldhStoreList .checkmore").hide();
		
		$("#shopList").html("");
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
		
	};
	
	//获取店铺状态帅选
	exports.getBusinessStatus=function(){
		
		return this.businessStatus==null?"":this.businessStatus;
		
	};
	
	//设置店铺特色
	exports.setSpeType=function(speTypeId,speTypeName){
		
		this.speTypeId=speTypeId;
		
		$("#speTypeName").html(base.subString(speTypeName,3));
		
		$("#ldhStoreList .storelist_filtration").removeClass("on");
				
		$("#ldhStoreList .checkmore").hide();
		
		$("#shopList").html("");
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
		
	};
	
	//获取店铺特色
	exports.getSpeType=function(){
		
		return this.speTypeId==null?"":this.speTypeId;
		
	};
	
	//设置商家类型帅选
	exports.setShopType=function(shopTypeId,shopTypeName){
		
		this.shopTypeId=shopTypeId;
		
		$("#ldhStoreList .storelist_classify").removeClass("on");

		$("#shopTypeName").html(shopTypeName);
		
		$("#ldhStoreList .checkmore").hide();
		
		$("#shopList").html("");
		
		exports.setCurrentPage(1);
		
		exports.getShopList();
		
	
	};
	
	//获取商家类型帅选
	exports.getShopType=function(){
		
		return this.shopTypeId==null?"":this.shopTypeId;
		
	};
	
	//设置搜索商家名称
	exports.setShopName=function(shopName){
		
		this.shopName=shopName;
		
	};
	
	//获取搜索商家名称
	exports.getShopName=function(){
		
		return this.shopName==null?"":this.shopName;
		
	};
	
	//查询商家分类
	exports.getShopTypeByParentId=function(){

		var typeId=exports.getShopType();
		
		var params = {
				parentId :typeId
		};
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopweb/ShopsType/showShopsTypes.do", params));
						
						
						if(data["code"]==1){
							
						//	{"shopTypeList":[{"mcc":0,"memo":"","parentId":"","shopTypeId":"1","shopTypeName":"全部","sort":0},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"6","shopTypeName":"中餐","sort":6},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"7","shopTypeName":"西餐","sort":7},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"8","shopTypeName":"日韩","sort":8},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"9","shopTypeName":"蛋糕面包","sort":9},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"10","shopTypeName":"甜品","sort":10},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"12","shopTypeName":"快餐","sort":12},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"13","shopTypeName":"其它餐饮","sort":13},{"mcc":0,"memo":"","parentId":"1","shopTypeId":"49","shopTypeName":"肉菜","sort":49}],"code":"1","message":"调用成功"}
							var htmlStr="";
							
							for (var i = 0; i < data["shopTypeList"].length; i++) {
								
								htmlStr += '<li onclick=$ldh_shop_list.setShopType("'+data["shopTypeList"][i]["shopTypeId"]+'","'+data["shopTypeList"][i]["shopTypeName"]+'")><span>'+data["shopTypeList"][i]["shopTypeName"]+'</span></li>';
							}
							
							$("#shopTypeList").html(htmlStr);

						}
						
						
					}))().start();
		
		
	};
	
	//搜索商家分类列表中的商家
	exports.searchShop=function(){
		
		var keyword=$("#search_shop_name").val();
		
		exports.setCurrentPage(1);
		
		exports.setShopName(keyword);
		
		exports.getShopList();
		
		
	};
	
	
	//查询商家特色类型
	exports.getSpeTypes=function(){
		
		eval(
				Wind.compile("async", function() {

					var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopapp/SpeType/getSpeTypes.do", ""));
					
					
					if(data["code"]==1){
						
						var htmlStr="";
						
						for (var i = 0; i < data["data"].length; i++) {
							
							htmlStr += '<li onclick=$ldh_shop_list.setSpeType("'+data["data"][i]["speTypeId"]+'","'+data["data"][i]["name"]+'")><span>'+data["data"][i]["name"]+'</span></li>';
						}
						
						$("#speTypeList").html(htmlStr);

					}
					
					
				}))().start();
	};
		
	//查询商家列表
	exports.getShopList=function(){
			
		var lng=base.getCookie("lng");
		
		var lat=base.getCookie("lat");
				
		var params = {
				currentPage :exports.getCurrentPage(),
				pageSize :pageSize,
				lng :lng,
				lat :lat,
				shopName:exports.getShopName(),
				typeId :exports.getShopType(),
				businessStatus :exports.getBusinessStatus(),
				sort :exports.getSort(),
				speTypeId:exports.getSpeType(),
				cityId:"",
				currentCityId:"",
				shopId:"",
				sign :MD5(pageSize+""+exports.getCurrentPage()+exports.getShopType()+lng+lat+""+exports.getShopName()+exports.getSpeType()+exports.getSort()+"")
		};
		base.showLoading();
		
			eval(
					Wind.compile("async", function() {

						var data = $await(async.DDF_AsyncRequest("POST", ctx + "/shopapp/Shop/shopList.do", params));
						
						if(data["code"]==1){
		
							var htmlStr="";
							
						/*	if(data["rflag"]==1){
								
								htmlStr='<li>找不到符合条件的商家.下面为你推荐优质商家.你也可以马上"<font color="#ed5d3a">开店进驻</font>"哦.</li>';
							}*/

							for (var i = 0; i < data["shopList"].length; i++) {
							
							var distance=data["shopList"][i]["distance"];
							
							var introduce=data["shopList"][i]["introduce"];
							
							var minAmount = data["shopList"][i]["minAmount"];
							
							var businessStatus = data["shopList"][i]["businessStatus"];

							var score=(data["shopList"][i]["score"]/5)*100;
							
							var businessHours=data["shopList"][i]["businessHours"];
							
							var range= data["shopList"][i]["range"];
							
							if(validate.isNull(introduce))
								introduce="暂无";
							
							if(validate.isNull(businessHours))
								businessHours="全天";
							
							if(distance.indexOf(">")>=0){
								
								distance=distance+"km";
								
							}else if(distance<1){
								
								distance=(distance*1000)+"m";
								
							}else{
								
								distance="约"+parseInt(distance)+"km";
							}
							
							if(minAmount=="" || minAmount=="0"){
								minAmount="起送金额不限";
							}else{
								minAmount=minAmount+"元起送";
							}

							if(range=="" || range=="0"){
								range="配送范围不限";
							}else{
								range=range/1000;
								range=range+"km内配送";
							}
								var shopName=encodeURI(data["shopList"][i]["shopName"]);
										
										htmlStr += '<a rel="external" href="'+ctx+'/h5/shopIndex/'+data["shopList"][i]["shopId"]+'.shtml"><li>';
										if(data["shopList"][i]["logoUrl"]!="" && data["shopList"][i]["logoUrl"].indexOf(".")>0){
											htmlStr += '<div class="store_list_logo"><img src="'+fileOpenUrl+data["shopList"][i]["logoUrl"]+'"/></div>';
										}else{
											htmlStr += '<div class="store_list_logo"><img src="'+ctx+'/v1/img/default_234_220.png"/></div>';
										}
										
										htmlStr += '<div class="store_list_info">';
										if(businessStatus=="1"){
											htmlStr += '<h3>'+data["shopList"][i]["shopName"]+'</h3>';
										}else{
											htmlStr += '<h3>'+data["shopList"][i]["shopName"]+'(<font color="#eb543f">休息中</font>)</h3>';
										}
										htmlStr += '<p><span class="store_star"><em style="width:'+score+'%"></em></span><span>'+base.getSpeHtml(data["shopList"][i]["speId"])+'</span></p>';
										htmlStr += '<p><span>'+minAmount+'</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span>'+range+'</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span>距离'+distance+'</span></p>';
										htmlStr += '</div>';                    
										htmlStr += '</li></a>';
										
										$("#shopList").append(htmlStr);
										
										htmlStr="";
							}
							
						
							var totals=data["totals"];
							var pages=data["pages"];
							if(totals==0){
								$("#shopList").append('<li>找不到符合条件的商家</li>');
							}
							if(exports.getCurrentPage()<pages){
								$("#ldhStoreList .checkmore").show();
							}else if(exports.getCurrentPage()==pages && pages>1){
								$("#ldhStoreList .checkmore").show();
								$("#ldhStoreList .checkmore").html("已到最后一页");
								$("#ldhStoreList .checkmore").removeAttr("onclick");
							}
							

						}
						
						base.cancelLoading();
						
						
					}))().start();
			
	};
	
	/**
	 * 加载更多
	 */
	exports.loadMoreShopList=function(){
		
		$("#ldhStoreList .checkmore").hide();
		
		exports.setCurrentPage(exports.getCurrentPage()+1)
		
		exports.getShopList();
	}
	
	
});

